<?php

class RegistrationController extends Controller
{
	public function actionIndex()
	{
		$model=new Client('registration');		

	    // uncomment the following code to enable ajax-based validation
	    
	    if(isset($_POST['ajax']) && $_POST['ajax']==='client-index-form')
	    {
	        echo CActiveForm::validate($model);
	        Yii::app()->end();
	    }
	    	   	  

	    if(isset($_POST['Client']))
	    {
	        $model->attributes=$_POST['Client'];	        
	        $model->setAttributes(array(
	        	'city_id' => Yii::app()->request->getParam('city', 1)
	        ));	     

	        if($model->validate()) {	        	
	            $model->save();

	            $identity = new ClientIdentity($_POST['Client']['email'], $_POST['Client']['password']);	            
	            if ($identity->authenticate()) {
	            	Yii::app()->user->login($identity);
	            }
	            $this->redirect(Yii::app()->user->returnUrl);	            
	        }
	    }
	    $this->render('index',array('model'=>$model));
	}

	public function actionGetCityByRegionId() {
		$data = City::model()->findAll('region_id=:region_id',
			array(':region_id' => Yii::app()->request->getParam('region_id')));

		$data = CHtml::listData($data, 'id', 'name');

		foreach ($data as $id => $name)
			echo CHtml::tag('option', array('value' => $id), CHtml::encode($name), true);
	}

	public function actionConfirm() {
	    $this->render('confirm');	
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}